package com.shujia.scala

import java.io.{BufferedReader, FileReader}
import java.util
import java.util.HashMap

object Demo03WordCountScala {
  def main(args: Array[String]): Unit = {
    // 1、读取文件
    val br: BufferedReader = new BufferedReader(new FileReader("scala/data/words.txt"))
    var line: String = br.readLine()

    val wordCntMap: util.HashMap[String, Integer] = new util.HashMap[String, Integer]

    // 2、依次处理每一行数据// 2、依次处理每一行数据
    while (line != null) {
      // 3、切分每一条数据
      for (word <- line.split(",")) {
        // 4、判断单词是否存在于map中
        if (wordCntMap.containsKey(word)) {
          // 存在就将value取出来并加一再写回去
          wordCntMap.put(word, wordCntMap.get(word) + 1)
        }
        else {
          // 不存在就直接将value置为1再写进去
          wordCntMap.put(word, 1)
        }
      }

      line = br.readLine()
    }

    // 5、打印结果// 5、打印结果
    import scala.collection.JavaConversions._
    for (kv <- wordCntMap.entrySet) {
      System.out.println(kv.getKey + "," + kv.getValue)
    }
  }
}
